package com.study.hot.jetpack.paging.paging3

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.hot.R
import com.study.hot.databinding.ItemDataBinding

/**
 * <pre>
 *     author : slx
 *     time   : 2021/02/02
 *     desc   :
 * </pre>
 */
class DataRecycleViewAdapter : PagingDataAdapter<DemoReqData.DataBean.DatasBean, RecyclerView.ViewHolder>(
        object : DiffUtil.ItemCallback<DemoReqData.DataBean.DatasBean>() {
            override fun areItemsTheSame(oldItem: DemoReqData.DataBean.DatasBean, newItem: DemoReqData.DataBean.DatasBean): Boolean {
                return oldItem.id == newItem.id
            }

            @SuppressLint("DiffUtilEquals")
            override fun areContentsTheSame(oldItem: DemoReqData.DataBean.DatasBean, newItem: DemoReqData.DataBean.DatasBean): Boolean {
                return oldItem == newItem
            }
        }
) {
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as DataViewHolder).binding.demoReaData = getItem(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return DataViewHolder(
                DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_data,
                        parent, false)
        )
    }

    inner class DataViewHolder(private val dataBinding: ItemDataBinding) :
            RecyclerView.ViewHolder(dataBinding.root) {
        var binding = dataBinding
    }
}